package com.example.test622.detail

import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.CompoundButton
import android.widget.Switch
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.mvi.BaseView
import com.example.common.mvi.BaseViewModel
import com.example.net.ConstantValue
import com.example.net.entity.SimpleVideoEntity
import com.example.test622.MyBaseCahe
import com.example.test622.R
import com.example.test622.databinding.ActivityDetailBinding
import com.google.android.material.tabs.TabLayout
import master.flame.danmaku.controller.DrawHandler
import master.flame.danmaku.danmaku.model.BaseDanmaku
import master.flame.danmaku.danmaku.model.DanmakuTimer
import master.flame.danmaku.danmaku.model.IDanmakus
import master.flame.danmaku.danmaku.model.android.BaseCacheStuffer
import master.flame.danmaku.danmaku.model.android.DanmakuContext
import master.flame.danmaku.danmaku.model.android.Danmakus
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser
import kotlin.random.Random

@Route(path = ConstantValue.VIDEO_DETAIL_PATH)
class DetailActivity : BaseView<ActivityDetailBinding, BaseViewModel<*, *>>() {
	@Autowired
	@JvmField
	var data: SimpleVideoEntity? = null
	private lateinit var sw:Switch
	private lateinit var detailDescriptFragment: DetailDescriptFragment
	private lateinit var detailCommentFragment: DetailCommentFragment
	private var v: SimpleVideoEntity? = null
	private lateinit var dmcontext: DanmakuContext
	private fun getDanmaKuParse(): BaseDanmakuParser {
		return object : BaseDanmakuParser(){
			override fun parse(): IDanmakus {
				return Danmakus()
			}
		}
	}
	private fun BaseCacheStufferss(): BaseCacheStuffer.Proxy =
		object : BaseCacheStuffer.Proxy(){
			override fun prepareDrawing(danmaku: BaseDanmaku?, fromWorkerThread: Boolean) {
			
			}
			
			override fun releaseResource(danmaku: BaseDanmaku?) {
				danmaku?.tag = null
			}
			
		}
	override fun initView() {
		sw = findViewById(R.id.sw_detail)
		initDanmu()
		ARouter.getInstance().inject(this)
		binding.video = data
		v = binding.video
		binding.tvDetail.setOnClickListener {
			Log.d("+++", "+initView: $data")
			ARouter.getInstance().build("/detail/video").withParcelable("date",data).navigation()
		}
		binding.tabDetail.addTab(binding.tabDetail.newTab().setText("简介"))
		binding.tabDetail.addTab(binding.tabDetail.newTab().setText("评论"))
		var bundle = Bundle()
		bundle.putParcelable("data", data)
		detailDescriptFragment = DetailDescriptFragment()
		detailDescriptFragment.arguments = bundle
		detailCommentFragment = DetailCommentFragment()
		detailCommentFragment.arguments = bundle
		supportFragmentManager.beginTransaction().replace(R.id.frame_detail, detailDescriptFragment) .commitAllowingStateLoss()
		binding.tabDetail.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
			override fun onTabSelected(tab: TabLayout.Tab?) {
				if (tab?.text == "简介") {
					supportFragmentManager.beginTransaction() .replace(R.id.frame_detail, detailDescriptFragment) .commitAllowingStateLoss()
				} else {
					supportFragmentManager.beginTransaction() .replace(R.id.frame_detail, detailCommentFragment).commitAllowingStateLoss()
				}
			}
			override fun onTabUnselected(tab: TabLayout.Tab?) {
			
			}
			override fun onTabReselected(tab: TabLayout.Tab?) {
			
			}
		})
		binding.tvDetail.setOnClickListener {
			val dmDialog = DanmuBottomSheetDialogFragment()
			dmDialog.setDanmuOnClickListener(object : DanmuBottomSheetDialogFragment.DanmuClickListener{
				override fun onclick() {
					val content = dmDialog.et.text.toString()
					if(content.isBlank()){
						return
					}
					AddDanmaku(false,content)
					dmDialog.close()
				}
			})
			dmDialog.show(supportFragmentManager,"DanmuBottomSheetDialogFragment")
		}
		
		binding.swDetail.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
			override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
				if(isChecked){
					val mdanmakuParser = getDanmaKuParse()
					binding.dmkDetailHome.prepare(mdanmakuParser,dmcontext)
					binding.dmkDetailHome.resume()
				}else{
					binding.dmkDetailHome.stop()
				}
			}
			
		})
		
	}
	
	private fun initDanmu() {
		//创建弹幕上下文
		//上下文设置
		dmcontext = DanmakuContext()
		dmcontext.setDuplicateMergingEnabled(true)
			.setScrollSpeedFactor(1.2f)
			.setScaleTextSize(1.2f)
			.setCacheStuffer(MyBaseCahe(this),BaseCacheStufferss())
		if(binding.dmkDetailHome != null){
			val danmakuparse = getDanmaKuParse()
			binding.dmkDetailHome.setCallback(object : DrawHandler.Callback{
				override fun prepared() {
					binding.dmkDetailHome.start()
				}
				
				override fun updateTimer(timer: DanmakuTimer?) {
				
				}
				
				override fun danmakuShown(danmaku: BaseDanmaku?) {
				
				}
				
				override fun drawingFinished() {
				
				}
			})
			//弹幕上下文
			binding.dmkDetailHome.prepare(danmakuparse,dmcontext)
			//弹幕缓存
			binding.dmkDetailHome.enableDanmakuDrawingCache(true)
		}
		mHandler.postDelayed(object : Runnable{
			override fun run() {
				AddDanmaku(false,"10+谶")
				mHandler.postDelayed(this,1000)
			}
		},1000)
	}
	private val mHandler =  Handler()
	
	private val mContentColorBg = arrayListOf("#0099ff", "#b9b9f1", "#f46c77")
	private fun AddDanmaku(isLive:Boolean,content:String){
		val danmu:BaseDanmaku = dmcontext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL)
		if(danmu != null || binding.dmkDetailHome != null){
			return
		}
		val map:MutableMap<String,Any> = HashMap(16)
		map["content"] = content
		var bitmap = BitmapFactory.decodeResource(resources,R.drawable.home_danmu)
		map["bitmap"] = bitmap
		var random = Random
		var randomnum = random.nextInt(mContentColorBg.size)
		var color = mContentColorBg.get(randomnum)
		map["color"] = color
		
		danmu.tag = map
		danmu.text = "我是一个弹幕"
		danmu.textColor = Color.WHITE
		danmu.textSize = 0f
		danmu.textShadowColor = 0
		danmu.padding = 10
		danmu.priority = 1
		danmu.isLive = isLive
		danmu.time = binding.dmkDetailHome.currentTime
		binding.dmkDetailHome.addDanmaku(danmu)
	
	}
	override fun initData() {
	
	}
}